<template>
  <div
    class="app-container"
    style="
    width: 92%;
    border-radius: 5px;
    height: 95%;
    margin: 20px auto;
    background-color: #f5f7f9;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;"
  >
    <div class="modal-box-ui">
      <el-form ref="passwordForm" :model="passwordForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">
        <div class="title-container">
          <h3 class="title">时序电源管理密码</h3>
        </div>
        <el-form-item prop="oldPassword">
          <span class="svg-container">
            时序电源管理密码
          </span>
          {{powerPasswd}}
        </el-form-item>

        <el-form-item prop="newPassword">
          <span class="svg-container">
            新密码
          </span>
          <el-input
            ref="newPassword"
            v-model="passwordForm.newPassword"
            placeholder="请输入新密码"
            name="newPassword"
            :type="passwordType"
            tabindex="1"
          />
        </el-form-item>
        <el-form-item prop="confirmPassword">
          <span class="svg-container">
            确认密码
          </span>
          <el-input
            ref="confirmPassword"
            v-model="passwordForm.confirmPassword"
            placeholder="请输入确认密码"
            name="confirmPassword"
            :type="passwordType"
            tabindex="1"
          />
        </el-form-item>
        <el-row>
          <el-col span="12" style="text-align: center;">
            <el-button
              :loading="loading"
              type="primary"
              style="text-align: center; width:80%;margin-bottom:30px;
            margin-top: 18px;"
              @click.native.prevent="handleConfirmChangePassword"
            >确定</el-button>
          </el-col>
          <el-col span="12" style="text-align: center;">
            <el-button
              :loading="loading"
              type="default"
              style="text-align: center;width:80%;margin-bottom:30px;
            margin-top: 18px;"
              @click.native.prevent="cancelChangePassword"
            >取消</el-button>
          </el-col>
        </el-row>
      </el-form>

    </div>

  </div>
</template>

<script>
import {changePowerPassword, getPowerPassword } from '@/api/user'

export default {
  name: 'PasswordManage',
  data() {
    return {
      powerPasswd: '',
      passwordForm: {
        oldPassword: '',
        newPassword: '',
        confirmPassword: ''
      },
      passwordType: 'password',
      loading: false
    }
  },
  created() {

  },
  mounted() {
    this.getOriginalPowerPassword()
  },
  methods: {
    getOriginalPowerPassword() {
      getPowerPassword().then(res => {
        console.log(res)
        const password = res.data
        this.powerPasswd = password.password
      })
    },
    handleConfirmChangePassword() {
      const h = this.$createElement
      const param = this.passwordForm
      if (param.newPassword === '') {
        this.$message({
          message: h('p', null, [
            h('span', null, '新密码不能为空 '),
            h('i', { style: 'color: teal' }, '')
          ])
        })
        return
      }
      if (param.confirmPassword === '') {
        this.$message({
          message: h('p', null, [
            h('span', null, '确认密码不能为空 '),
            h('i', { style: 'color: teal' }, '')
          ])
        })
        return
      }
      if (param.confirmPassword !== param.newPassword) {
        this.$message({
          message: h('p', null, [
            h('span', null, '确认密码与新密码不同 '),
            h('i', { style: 'color: teal' }, '')
          ])
        })
        return
      }
      changePowerPassword(param).then(res => {
        this.$message({
          message: h('p', null, [
            h('span', null, '密码更新成功 '),
            h('i', { style: 'color: teal' }, '')
          ])
        })
        this.getOriginalPowerPassword()
      })
    },
    async cancelChangePassword() {
      await this.$store.dispatch('user/logout')
      this.$router.push('/login')
    }

  }
}
</script>
<style lang="scss" scoped>
.login-form {
  position: absolute;
  width: 380px;
  max-width: 100%;
  // padding: 160px 35px 0;
  margin: 34px 115px;;
  overflow: hidden;
}
.modal-box-ui{
  width: 500px;
  margin-left: auto;
  margin-right: auto;
  height: auto;
  display: inline-block;
  background-color: #ffffff;
}
.btn{
  width: 10px;
  height: 10px;
  background-image: url("../../assets/imgs/u749.png");
}
::v-deep .el-input__inner {
  height: 32px;
}
</style>
